import urllib.error
import urllib.parse
import urllib.request

# data
data = bytes(urllib.parse.urlencode({'word': 'hello'}), encoding='utf8')
response = urllib.request.urlopen('http://httpbin.org/post', data=data, timeout=1)
print(response.read())

# timeout \ try except
try:
    response = urllib.request.urlopen('http://httpbin.org/get', timeout=0.01)
    print(response.read())
except urllib.error.URLError:
    print('Time out')

response = urllib.request.urlopen('https://www.python.org')
print(response.getheader('Server'))

# Request
request = urllib.request.Request('https://www.python.org')
response = urllib.request.urlopen(request)
print(response.read().decode('utf-8'))
